package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.intercept.UserInterceptor;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @Description CustomerReservationServiceImpl
 * @Author KangChi Wang
 * @Date 2024-08-31
 */
@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {
    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    /**
     * 分页查询预约
     *
     * @param pageNum
     * @param pageSize
     * @param status
     * @return {@link PageResponse }<{@link ReservationVo }>
     */
    @Override
    public PageResponse<ReservationVo> selectByPage(Integer pageNum, Integer pageSize, Integer status) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        //接收数据
        Page<ReservationVo> page = customerReservationMapper.selectByPage(status);
        PageResponse<ReservationVo> pages = new PageResponse<>(page);
        pages.setRecords(page.getResult());
        return pages;

    }

    /**
     * 新增预约
     *
     * @param reservationVo
     */
    @Override
    public void add(ReservationVo reservationVo) {
        reservationVo.setStatus(0);
        customerReservationMapper.add(reservationVo);
    }

    /**
     * 取消预约
     *
     * @param id
     */
    @Override
    public void cancelAppointment(Long id) {

            customerReservationMapper.cancelAppointment(id);

    }

    /**
     * 查询取消预约次数
     *
     * @return {@link Integer }
     */
    @Override
    public Integer cancelCount() {
        Long userId = UserThreadLocal.getUserId();
        //得到当天零点的时间戳
        long now = System.currentTimeMillis();
        long daySecond = 24L * 60 * 60 * 1000; // 一天的毫秒值
        long start = now - (now + TimeZone.getDefault().getRawOffset()) % daySecond;
        LocalDateTime start1 = LocalDateTimeUtil.of(start);

        Integer count = customerReservationMapper.cancelCount(userId,start1);
        return count;
    }

    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return {@link List }<{@link TimeCountVo }>
     */
    @Override
    public List<TimeCountVo> numberRemaining(Long time) {
        LocalDateTime startTime = LocalDateTimeUtil.of(time);
        LocalDateTime endTime = startTime.plusHours(24);
        List<TimeCountVo> list = customerReservationMapper.numberRemaining(startTime, endTime);
        for (TimeCountVo timeCountVo : list) {
           timeCountVo.setCount(6-timeCountVo.getCount());
        }
        return list;
    }

    @Override
    public void scheduledTasks() {
        long now = System.currentTimeMillis();
        long daySecond = 24L * 60 * 60 * 1000; // 一天的毫秒值
        long dayTime = now - (now + TimeZone.getDefault().getRawOffset()) % daySecond;

        LocalDateTime start = LocalDateTimeUtil.of(dayTime);
        LocalDateTime end = LocalDateTime.now();
        customerReservationMapper.updateStatus(start,end);




    }
}
